<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="供应商名称" prop="supplierName" label-width="auto">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商名称"
          clearable
          maxlength="20"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商分类" prop="supplierCategory" label-width="auto">
        <el-select v-model="queryParams.supplierCategory" placeholder="请选择供应商分类" clearable>
          <el-option
            v-for="dict in dict.type.supplier_classification"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="供应商地址" prop="supplierAddress" label-width="auto">
        <el-cascader
          style="width: 100%"
          expand-trigger="hover"
          ref="cascaderAddr"
          size="small"
          :options="options"
          v-model="querySelectedOptions"
          @change="queryhandleChange"
          @keyup.enter.native="handleQuery"
        >
          <template slot-scope="{ node, data }">
            <span>{{ data.label }}</span>
            <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
          </template>
        </el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:supplier:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:supplier:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:supplier:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="supplierList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="供应商名称" align="center" prop="supplierName"/>
      <el-table-column label="联系人" align="center" prop="supplierContacts"/>
      <el-table-column label="联系人电话" align="center" prop="supplierPhone"/>
      <el-table-column label="供应商分类" align="center" prop="supplierCategory">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.supplier_classification" :value="scope.row.supplierCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="供应商地址" align="center" prop="supplierAddress"/>
      <el-table-column label="供应商地址" align="center" prop="detailedAddress"/>
      <el-table-column label="供应商开户银行" align="center" prop="supplierBank"/>
      <el-table-column label="供应商银行账户" align="center" prop="supplierBankAccount"/>
      <el-table-column label="供应商税号" align="center" prop="supplierDutyParagraph"/>
      <el-table-column label="备注信息" align="center" prop="remarks" show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:supplier:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:supplier:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal = "false" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="supplierName">
          <el-input v-model="form.supplierName" placeholder="请输入供应商名称" maxlength="20"/>
        </el-form-item>
        <el-form-item label="联系人" prop="supplierContacts">
          <el-input v-model="form.supplierContacts" placeholder="请输入联系人" maxlength="20"/>
        </el-form-item>
        <el-form-item label="联系方式" prop="supplierPhone">
          <el-input v-model="form.supplierPhone" placeholder="请输入联系人电话" maxlength="11" @input="v => form.supplierPhone = v.replace(/[^\d]/g,'')"/>
        </el-form-item>
        <el-form-item label="分类" prop="supplierCategory">
          <el-select v-model="form.supplierCategory" placeholder="请选择供应商分类" style="width: 100%">
            <el-option
              v-for="dict in dict.type.supplier_classification"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="supplierAddress" :label-position="labelPosition" label-width="80px">
          <el-cascader
            style="width: 100%"
            expand-trigger="hover"
            ref="cascaderAddr"
            size="small"
            :options="options"
            v-model="selectedOptions"
            @change="handleChange">
            <template slot-scope="{ node, data }">
              <span>{{ data.label }}</span>
              <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
            </template>
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="detailedAddress" label-height="100px">
          <el-input v-model="form.detailedAddress" placeholder="请输入供应商详细地址" maxlength="40"/>
        </el-form-item>
        <el-form-item label="开户银行" prop="supplierBank" label-height="100px">
          <el-input v-model="form.supplierBank" placeholder="请输入供应商开户银行" maxlength="20"/>
        </el-form-item>
        <el-form-item label="银行账号" prop="supplierBankAccount">
          <el-input v-model="form.supplierBankAccount" placeholder="请输入供应商银行账户" @input="v => form.supplierBankAccount = v.replace(/[^\d]/g,'')"
                    maxlength="20"/>
        </el-form-item>
        <el-form-item label="税号" prop="supplierDutyParagraph">
          <el-input v-model="form.supplierDutyParagraph" placeholder="请输入供应商税号" oninput="value=value.replace(/[^\d\w]/g,'')" maxlength="30"/>
        </el-form-item>
        <el-form-item label="备注信息" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" maxlength="250" placeholder="请输入备注信息"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listSupplier, getSupplier, delSupplier, addSupplier, updateSupplier} from "@/api/erp/supplier";
import {CodeToText, regionData} from "element-china-area-data";

export default {
  name: "Supplier",
  dicts: ['supplier_classification'],
  data() {
    return {
      //查询条件地址
      querySelectedOptions: [],
      //省市区地址信息
      options: regionData,
      //表单地址信息
      selectedOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中数组names
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商管理表格数据
      supplierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierName: null,
        supplierContacts: null,
        supplierPhone: null,
        supplierCategory: null,
        supplierAddress: null,
        supplierBank: null,
        supplierBankAccount: null,
        supplierDutyParagraph: null,
        detailedAddress: null,
        remarks: null,
        creatTime: null,
        creatUser: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        supplierName: [
          {required: true, message: "供应商名称不能为空", trigger: "blur"}
        ],
        supplierContacts: [
          {required: true, message: "联系人不能为空", trigger: "blur"}

        ],
        supplierPhone: [
          {required: true, message: "联系人电话不能为空", trigger: "blur"},
          {
            required: true,
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        supplierCategory: [
          {required: true, message: "供应商分类不能为空", trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    /** elementUi选择地址 */
    handleChange(value) {
      this.selectedOptions = value
      var name = ''
      this.selectedOptions.map(item =>
        name += CodeToText[item] + '') //将省市区三个拼接一起
      this.form.province = value[0];
      this.form.city = value[1];
      this.form.area = value[2];
      this.form.supplierAddress = name;
    },

    queryhandleChange(value) {
      this.querySelectedOptions = value
      var name = ''
      this.querySelectedOptions.map(item =>
        name += CodeToText[item] + '') //将省市区三个拼接一起
      this.queryParams.supplierAddress = name;
    },

    /** 查询供应商管理列表 */
    getList() {
      this.loading = true;
      listSupplier(this.queryParams).then(response => {
        this.supplierList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        supplierName: null,
        supplierContacts: null,
        supplierPhone: null,
        supplierCategory: null,
        supplierAddress: null,
        supplierBank: null,
        supplierBankAccount: null,
        supplierDutyParagraph: null,
        detailedAddress: null,
        remarks: null,
        creatTime: null,
        updateTime: null,
        creatUser: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.querySelectedOptions = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item => item.supplierName)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.selectedOptions = "";
      this.open = true;
      this.title = "添加供应商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      if (row.province != null && row.city != null && row.area != null) {
        this.selectedOptions = [row.province.toString(), row.city.toString(), row.area.toString()]
      }
      getSupplier(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改供应商";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSupplier(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSupplier(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const names = row.supplierName || this.names;
      this.$modal.confirm('是否确认删除供应商"' + names + '"？').then(function () {
        return delSupplier(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/supplier/export', {
        ...this.queryParams
      }, `supplier_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
